package com.wzr.furerp.workshop.controller;

import com.google.gson.Gson;
import com.wzr.furerp.workshop.dao.entity.Workshop;
import com.wzr.furerp.workshop.service.WorkshopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/furerp/workshop")
public class WorkShopController {

	private static final String FURERP = "/furerp";
	private static final String WORKSHOP_MANAGE = FURERP + "/workshop/manage";
	private static final String WORKSHOP_ADD = FURERP + "/workshop/add";
    /**
     * 导出字段
     */
    public static final String EXPORT_FIELD = "exportField";
	public static final String EXPORT_CONTENT = "exportContent";

	@Autowired
	private WorkshopService workshopService;
	
	@RequestMapping(value = "/manage", method = RequestMethod.GET)
	public void workshopManage(){
		return ;
	}

	@RequestMapping(value = "/getWorkshopJSONObject", method = RequestMethod.GET)
	@ResponseBody
	public String getWorkshopList(){
        // 用于返回前台的JSON对象
        Map<String,List> joRe = new HashMap<>();
        // 按显示顺序填入字段名
		ArrayList<ArrayList<String>> jaField = jaSeekWsField();
        joRe.put(EXPORT_FIELD, jaField);

		ArrayList<Map<String,String>> ja = convertWsListToJSONArray();
        joRe.put(EXPORT_CONTENT, ja);

		Gson gson = new Gson();

		return gson.toJson(joRe);
	}

    /**
     * 查询结果的字段名称用于前端取数据(作为JSON的KEY)
     * 按客户代码查询时,加入2个位置属性
     * @return
     */
    private ArrayList<ArrayList<String>> jaSeekWsField() {
		ArrayList<ArrayList<String>> ja = new ArrayList<>();
        putOneField(ja, "wsId","部门编号");
        putOneField(ja, "wsName","部门名称");
        return ja;
    }


    /**
     * 将字段代码和名称加入JSONArray
     * @param ja JSONArray
     * @param fieldCode 字段代码
     * @param fieldName 字段名称
     */
    private void putOneField(ArrayList<ArrayList<String>> ja, String fieldCode, String fieldName) {
        ArrayList<String> jone = new ArrayList<>();
        jone.add(fieldCode);
        jone.add(fieldName);
        ja.add(jone);
    }

    /**
     * 生成JSON格式的车间对象列表
     * @param pw
     */
//	@RequestMapping(value = "/getwsjsonlist", method = RequestMethod.GET)
//	public void getWsJSONList(PrintWriter pw){
//		return;
//	}

	/**
	 * 将所有车间对象转换成JSONArray
	 * @return
	 */
	private ArrayList<Map<String,String>> convertWsListToJSONArray() {
		List<Workshop> workshopList = workshopService.getAll();
		ArrayList<Map<String,String>> ja = new ArrayList<>();
		for(Workshop ws : workshopList){
			Map<String,String> jo = new HashMap<>();
			jo.put("wsId", String.valueOf(ws.getWsId()));
			jo.put("wsName", ws.getWsName());
			ja.add(jo);
		}
		return ja;
	}

	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String workshopAddView(Model model){
		model.addAttribute("workShop",new Workshop());
		return WORKSHOP_ADD;
	}
	
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String workshopAddSubmit(Workshop workShop){
		workshopService.add(workShop);
		return InternalResourceViewResolver.REDIRECT_URL_PREFIX + WORKSHOP_MANAGE;
	}
	
	@RequestMapping(value = "delete", method = RequestMethod.GET)
	public String workshopDelete(Workshop workShop){
		workshopService.delete(workShop);
		return InternalResourceViewResolver.REDIRECT_URL_PREFIX + WORKSHOP_MANAGE;
	}
}
